package cn.edu.jxau.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

/**
 * 符号图
 * 
 * @author 付大石
 */
public class SymbolGraph {
    
    public static void main(String[] args) throws IOException {
        
        SymbolGraph symbolGraph = new SymbolGraph(new File("C:\\Users\\PC-Clive\\Desktop\\data.csv"),"\t");
        String name = "JFK";
        for(int v : symbolGraph.graph().adj(symbolGraph.index(name))) {
            System.out.print(symbolGraph.name(v)+" ");
        }
        System.out.println("\n---");
        name = "LAX";
        for(int v : symbolGraph.graph().adj(symbolGraph.index(name))) {
            System.out.print(symbolGraph.name(v)+" ");
        }
    }
    
    private HashMap<String, Integer> map; // 符号与索引的映射
    private String[] keys; // 索引与符号的映射
    private Graph g;

    /**
     * 读取{@param file}中的数据，每行用{@param sp}分割
     * @param file
     * @param sp
     * @throws IOException 
     * @throws  
     */
    public SymbolGraph(File file, String sp) throws IOException {

        // 构造符号与索引的映射 //
        try (FileReader fileReader = new FileReader(file); BufferedReader reader = new BufferedReader(fileReader)) {
            map = new HashMap<>();
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] data = line.split(sp);
                for (String str : data) {
                    if (!map.containsKey(str)) {
                        map.put(str, map.size());
                    }
                }
            }

        } catch (IOException e) {
            throw new IOException("文件读取失败,文件路径：" + file.getCanonicalPath(), e);
        }

        // 构造索引与符号的映射 //
        keys = new String[map.size()];
        for (String str : map.keySet()) {
            keys[map.get(str)] = str;
        }

        // 构造图 //
        try (FileReader fileReader = new FileReader(file); BufferedReader reader = new BufferedReader(fileReader)) {
            g = new UndirectedGraph(keys.length);
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] data = line.split(sp);
                int v = map.get(data[0]);
                for (int i = 1; i < data.length; i++) {
                    g.addEdge(v, map.get(data[i]));
                }
            }
        } catch (IOException e) {
            throw new IOException("文件读取失败,文件路径：" + file.getCanonicalPath(), e);
        }
    }

    /**
     * 判断图中是否包含这个符号顶点
     * @param name
     * @return
     */
    public boolean contains(String name) {
        return map.containsKey(name);
    }

    /**
     * 获取符号对应的顶点的索引
     * @param name
     * @return
     */
    public int index(String name) {
        return map.get(name);
    }

    /**
     * 获取索引对应的顶点的符号
     * @param index
     * @return
     */
    public String name(int index) {
        return keys[index];
    }
    
    /**
     * 获取图
     * @return
     */
    public Graph graph() {
        return g;
    }
}